<?php

ini_set('display_errors', 1);
ini_set('memory_limit', -1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\activity\SNJActivityRecord;
use Doraemon\cache\UserCounter;
use Doraemon\tools\Hhzurl;

$path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'snjDesigner' . DIRECTORY_SEPARATOR;

downSNJImg($path);//生成wiki图片;

$data = getSNJData($path);//获取数据

exportData($data);//导出excel

function getSNJData($path)
{
    $data = [];
    $list = SNJActivityRecord::getRecordListByCondition(['type' => SNJActivityRecord::DESIGN_TYPE]);
    foreach ($list as $row) {
        $userCounter = UserCounter::getOne($row['uid']);
        $extend_info = json_decode($row['extend_info'], true);
        $row = array_merge($row, $extend_info);
        $data[] = [
            'id' => $row['id'],
            'uid' => $row['uid'],
            'interact_num' => $userCounter['beliked'] + $userCounter['befavorited'],
            'user_link' => Hhzurl::h5Person($row['uid']),
            'area_name' => $row['area_name'],
            'working_year' => $row['working_year'],
            'deco_style' => $row['deco_style'],
            'award_experience' => $row['award_experience'],
            'floor_path' => $path . $row['id'],
            'effect_picture' => $row['effect_picture'],
            'addtime' => $row['addtime'],

            //area_name 所在地域
//working_year 从业年限
//deco_style 擅长风格
//award_experience 获奖经历
//effect_picture 设计效果图


        ];
    }
    return $data;
}


function exportData($data)
{
    $file = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'snjDesigner' . '-' . date('YmdHis') . '-snj.csv';
    var_dump('开始导出文件:' . $file);
    $objPHPExcel = new \PHPExcel();
    $objWriter = new \PHPExcel_Writer_Excel2007($objPHPExcel);
    $objActSheet = $objPHPExcel->getActiveSheet();
    $objActSheet->setCellValue('A1', '用户Id');
    $objActSheet->setCellValue('B1', '用户被交互量');
    $objActSheet->setCellValue('C1', '用户主页链接');
    $objActSheet->setCellValue('D1', '所在地域');
    $objActSheet->setCellValue('E1', '从业年限');
    $objActSheet->setCellValue('F1', '擅长风格');
    $objActSheet->setCellValue('G1', '获奖经历');
    $objActSheet->setCellValue('H1', '设计效果图');
    $objActSheet->setCellValue('I1', '提交时间');
    $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);
    $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(30);
    $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
    $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
    $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(45);
    $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(20);
    $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(50);
    $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(20);
    $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(20);
    $objPHPExcel->getActiveSheet()->getStyle('H:I')->getAlignment()->setWrapText(TRUE);

    $objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(25);//行高
    foreach ($data as $k => $v) {
        $k += 2;
        $objActSheet->getRowDimension($k)->setRowHeight(100);
        $objActSheet->setCellValue('A' . $k, $v['uid']);
        $objActSheet->setCellValue('B' . $k, $v['interact_num']);
        $objActSheet->setCellValue('C' . $k, $v['user_link']);
        $objActSheet->setCellValue('D' . $k, $v['area_name']);
        $objActSheet->setCellValue('E' . $k, $v['working_year']);
        $objActSheet->setCellValue('F' . $k, $v['deco_style']);
        $objActSheet->setCellValue('G' . $k, $v['award_experience']);

        if ($v['floor_path']) {
            $objDrawing[$k] = new PHPExcel_Worksheet_Drawing();
            $objDrawing[$k]->setPath($v['floor_path']);
            $objDrawing[$k]->setHeight(100);
            $objDrawing[$k]->setWidth(100);
            $objDrawing[$k]->setCoordinates('H' . $k);
            $objDrawing[$k]->setOffsetX(100);
            $objDrawing[$k]->setRotation(20);
            $objDrawing[$k]->getShadow()->setVisible(true);
            $objDrawing[$k]->getShadow()->setDirection(50);
            $objDrawing[$k]->setWorksheet($objPHPExcel->getActiveSheet());
        }
        $objActSheet->setCellValue('I' . $k, $v['addtime']);
    }
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
    $objWriter->save($file);
}

function downSNJImg($path)
{
    $list = SNJActivityRecord::getRecordListByCondition(['type' => SNJActivityRecord::DESIGN_TYPE]);
    foreach ($list as $row) {
        $filename = $path . $row['id'];
        $extend_info = json_decode($row['extend_info'], true);
        $url = $extend_info['effect_picture'];
        if (!file_exists($filename)) {
            download($url, $filename);
        }
    }
}

function download($url, $filename)
{
    var_dump('下载三棱镜广告图片');
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 信任任何证书
    $file = curl_exec($ch);
    curl_close($ch);
    $resource = fopen($filename, 'a');
    fwrite($resource, $file);
    fclose($resource);
    return $filename;
}
